package com.xb.cloud.merchantapi.product.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xb.cloud.gateway.client.ReturnCodes;
import com.xb.cloud.gateway.client.ReturnUtils;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.merchantapi.product.service.StatementApiService;
import com.xb.cloud.order.service.client.StatementService;
import com.xb.cloud.product.merchant.service.client.EndMerchantUserService;
import com.xb.cloud.product.merchant.service.client.bo.EndMerchantUserBo;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;

@Service
public class StatementApiServiceImpl implements StatementApiService {
	@Resource
	private EndMerchantUserService endMerchantUserService;

	@Resource
	StatementService statementService;

	@Override
	public RtnInfo getMerchantFinaceInfo(EndMerchantUserBo merchantUserBo) {

		ServiceReturnInfo merchantReturnInfo = endMerchantUserService.getEndMerchantUserById(merchantUserBo.getId());
		if (ServiceReturnUtils.isSuccess(merchantReturnInfo)) {
			if (merchantReturnInfo.getData() == null) {
				return ReturnUtils.error(ReturnCodes.MERCHANT_NOT_EXIST);
			} else {
				merchantUserBo = (EndMerchantUserBo) merchantReturnInfo.getData();
			}
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}

		ServiceReturnInfo merchantFinaceInfo = statementService.getMerchantFinaceInfo(merchantUserBo);

		return ReturnUtils.success(merchantFinaceInfo.getData());
	}

	@Override
	public RtnInfo getTradeInfo(EndMerchantUserBo merchantUserBo, Date startTimeString, Date endTimeString) {
		ServiceReturnInfo merchantReturnInfo = endMerchantUserService.getEndMerchantUserById(merchantUserBo.getId());
		if (ServiceReturnUtils.isSuccess(merchantReturnInfo)) {
			if (merchantReturnInfo.getData() == null) {
				return ReturnUtils.error(ReturnCodes.MERCHANT_NOT_EXIST);
			} else {
				merchantUserBo = (EndMerchantUserBo) merchantReturnInfo.getData();
			}
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
		ServiceReturnInfo returnInfo = statementService.getTradeInfo(merchantUserBo,  startTimeString,  endTimeString);
		return ReturnUtils.success(returnInfo.getData());
	}

}
